<div class="box">
    <div class="heading">
        <h1><img alt="" src="<?php echo base_url('assets/admin/images/user.png'); ?>">Update Users</h1>
        <div class="buttons">
            <a class="button" href="#" id="save" onClick="$('#user_edit_form').submit()"><span>Save</span></a>
        </div>
    </div>
    <div class="content">
        <?php echo form_open(null, 'id="user_edit_form"'); ?>
			<div class="tabs">
                <ul class="htabs">
                    <li><a href="#edit-user-tab">Edit User</a></li>
                    <li><a href="#password-tab">Password</a></li>
                </ul>
				<div id="edit-user-tab">
					<div class="form">
						<div class="field_spacing">
							<?php echo form_label('<span class="required">*</span> Group:', 'groups'); ?>
							<select name="group_id" id="group_id">
								<?php 
									foreach($groups as $group)
									{	
										if($user['groupid']==$group['group_id'])
										{
											echo "<option  value='".$group['group_id']."' gtype='".$group['group_type']."' selected='selected' >".$group['group_name']."</option>";
										}
										else
										{
											echo "<option  value='".$group['group_id']."' gtype='".$group['group_type']."'>".$group['group_name']."</option>";
										}
									}								
								?>
							</select>
							<input type="hidden" name="group_type" id="group_type" />
						</div>
						<div class="field_spacing">
							<?php echo form_label('<span class="required">*</span> Email:', 'email'); ?>
							<?php echo form_input(array('id' => 'email', 'name' => 'email', 'value' => set_value('email',$user['email']))); ?>
						</div>
						
						<div class="field_spacing">
							<?php echo form_label('<span class="required">*</span> First Name:', 'first_name'); ?>
							<?php echo form_input(array('id' => 'firstname', 'name' => 'firstname', 'value' => set_value('firstname',$user['firstname']))); ?>
						</div>
						<div class="field_spacing">
							<?php echo form_label('<span class="required">*</span> Last Name:', 'last_name'); ?>
							<?php echo form_input(array('id' => 'lastname', 'name' => 'lastname', 'value' => set_value('lastname',$user['lastname']))); ?>
						</div>
						<div class="field_spacing">
							<?php echo form_label('Phone:', 'phone'); ?>
							<?php echo form_input(array('id' => 'phoneno', 'name' => 'phoneno', 'value' => set_value('phoneno',$user['phoneno']))); ?>
						</div>
						<div class="field_spacing">
							<?php echo form_label('Address:', 'address'); ?>
							<?php echo form_input(array('id' => 'address', 'name' => 'address', 'value' => set_value('address',$user['address']))); ?>
						</div>
						<div class="field_spacing">
							<?php echo form_label('Address 2:', 'address2'); ?>
							<?php echo form_input(array('id' => 'address2', 'name' => 'address2', 'value' => set_value('address2',$user['address2']))); ?>
						</div>
						<div class="field_spacing">
							<?php echo form_label('City:', 'city'); ?>
							<?php echo form_input(array('id' => 'city', 'name' => 'city', 'value' => set_value('city',$user['city']))); ?>
						</div>
						<div class="field_spacing">
							<?php echo form_label('Country:', 'country'); ?>
							<select name="country_id" id="country_id">
								<?php foreach ($countries as $country) {
								if($user['country_id']==$country['country_id'])
								{
								?>
								<option value="<?php echo $country['country_id']; ?>" selected="selected" ><?php echo $country['name']; ?></option>
								<?php }else{ ?>
								<option value="<?php echo $country['country_id']; ?>" ><?php echo $country['name']; ?></option>
								<?php }}?>
							</select>
						</div>
						<div class="field_spacing">
							<?php echo form_label('State:', 'state'); ?>
							<select name="state_id" id="state_id">
								<option value="0">Select state</option>
							</select>
						</div>
						<div class="field_spacing">
							<?php echo form_label('Zip:', 'zip'); ?>
							<?php echo form_input(array('id' => 'zip', 'name' => 'zip', 'value' => set_value('zip',$user['zip']))); ?>
						</div>
						<div>
							<?php echo form_label('Status: <span class="help">Allow user to log in.</span>', 'enabled'); ?>
							<span>
								<?php echo form_radio(array('id' => 'status_enabled', 'name' => 'enabled', 'value' => '1', 'checked' => set_radio('enabled', '1',($user['activated']==1) ? TRUE : TRUE))); ?>
								<label for="status_enabled">Enabled</label>
								<?php echo form_radio(array('id' => 'status_disabled', 'name' => 'enabled', 'value' => '0', 'checked' => set_radio('enabled', '0',($user['activated']==0) ? TRUE : FALSE))); ?> 
								<label for="status_disabled">Disabled</label>
							</span>
						</div>
					</div>
				</div>
				<div id="password-tab">
					<div class="form">
						<div class="field_spacing">
							<?php echo form_label('<span class="required">*</span> Password:', 'password')?>
							<?php echo form_password(array('id' => 'password', 'name' => 'password')); ?>
						</div>
						<div class="field_spacing">
							<?php echo form_label('<span class="required">*</span> Confirm Password:', 'confirm_password'); ?>
							<?php echo form_password(array('id' => 'confirm_password', 'name' => 'confirm_password')); ?>
						</div>
					</div>
				</div>
			</div>
            <div class="clear"></div>
        <?php echo form_close(); ?>
    </div>
</div>

<script type="text/javascript">
$(document).ready(function() {
        $( ".tabs" ).tabs();
});
$('select[name=\'country_id\']').bind('change', function() {
	$.ajax({
		url: '<?php echo base_url();?>ajax/state/' + $("#country_id").val(),
		dataType: 'json',
		beforeSend: function() {
			$('select[name=\'country_id\']').after('<span class="wait">&nbsp;<img src="catalog/view/theme/default/image/loading.gif" alt="" /></span>');
		},
		complete: function() {
			$('.wait').remove();
		},			
		success: function(json) {
			//alert(json);
			
			html = "<option value=''>Select State</option>";
			if (json['zone'] != '') {
				for (i = 0; i < json['zone'].length; i++) {
        			html += "<option value='" + json['zone'][i]['zone_id'] + "'";
					if (json['zone'][i]['zone_id'] == '<?php echo $user['state_id']; ?>') {
	      				html += ' selected="selected"';
	    			}
	    			html += '>' + json['zone'][i]['name'] + '</option>';
				}
			} else {
				html += '<option value="0" selected="selected">Select state</option>';
			}
			
			$('select[name=\'state_id\']').html(html);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

$('select[name=\'country_id\']').trigger('change');

$('select[name=\'group_id\']').bind('change', function() {
	$("#group_type").val($('option:selected', this).attr('gtype'));
});
$('select[name=\'group_id\']').trigger('change');
//-->
</script>
